package com.yanqu.road.server.gameplayer.module.activity.fund;

import com.yanqu.road.dao.impl.activity.fund.UserActivityFundDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.fund.UserActivityFundData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityFundModule extends GeneralModule {
    public ActivityFundModule(GamePlayer player) {
        super(player);
    }

    //key1 activityId
    private Map<Integer, UserActivityFundData> userActivityFundDataMap = new ConcurrentHashMap<>();

    @Override
    public boolean loadData() {
        Map<Integer, UserActivityFundData> tempMap = new ConcurrentHashMap<>();
        List<UserActivityFundData> tempDataList = new UserActivityFundDataDaoImpl().getUserActivityFundDataList(getUserId());
        for (UserActivityFundData data : tempDataList) {
            ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(data.getActivityId());
            if (openActivityInfo != null) {
                tempMap.put(data.getActivityId(), data);
            }
        }

        userActivityFundDataMap = tempMap;

        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        List<UserActivityFundData> list = getAllUserActivityFundDataList();
        for (UserActivityFundData data : list) {
            if (data.isInsertOption()) {
                new UserActivityFundDataDaoImpl().add(data);
            } else if (data.isUpdateOption()) {
                new UserActivityFundDataDaoImpl().update(data);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        syncUserData();
    }

    public List<UserActivityFundData> getAllUserActivityFundDataList() {
        List<UserActivityFundData> dataList = new ArrayList<>();
        synchronized (userActivityFundDataMap) {
            dataList.addAll(userActivityFundDataMap.values());
        }
        return dataList;
    }

    public void syncUserData() {
        CommonActivityProto.CommonActivityFundUserSyncMsg.Builder syncMsg = CommonActivityProto.CommonActivityFundUserSyncMsg.newBuilder();
        List<UserActivityFundData> list = getAllUserActivityFundDataList();
        for (UserActivityFundData data : list) {
            CommonActivityProto.CommonActivityFundTemp.Builder temp = CommonActivityProto.CommonActivityFundTemp.newBuilder();
            temp.setActivityId(data.getActivityId());
            temp.setProgress(data.getProgress());

            for (Integer buyMallKeyId : data.getBuyMallKeyIds()) {
                MallInfo mallInfo = NormalActivityMgr.getRMBActivityMallInfoByGrade(data.getActivityId(), buyMallKeyId);
                if (mallInfo != null) {
                    temp.addBuyMallIds(mallInfo.getMallId());
                }
            }

            syncMsg.addActivityFundList(temp);
        }
        player.sendPacket(ClientProtocol.U_COMMON_ACTIVITY_FUND_USER_DATA_SYNC, syncMsg);

    }


    public boolean isBuy(MallInfo mallInfo) {
        int activityId = mallInfo.getActivityId();
        int mallKeyId = Integer.valueOf(mallInfo.getExtendParam());

        UserActivityFundData userActivityFundData = userActivityFundDataMap.get(activityId);
        if (userActivityFundData == null) {
            return false;
        }
        return userActivityFundData.isBuyByMallKey(mallKeyId);
    }

    private UserActivityFundData getUserActivityFundDataNewIfNull(int activityId) {
        UserActivityFundData userActivityFundData = userActivityFundDataMap.get(activityId);
        if (userActivityFundData == null) {
            synchronized (userActivityFundDataMap) {
                userActivityFundData = userActivityFundDataMap.get(activityId);
                if (userActivityFundData == null) {
                    userActivityFundData = new UserActivityFundData(activityId, getUserId());
                    userActivityFundDataMap.put(activityId, userActivityFundData);
                }
            }
        }
        return userActivityFundData;
    }

    public void buy(MallInfo mallInfo) {
        int mallKeyId = Integer.valueOf(mallInfo.getExtendParam());
        UserActivityFundData userActivityFundData = getUserActivityFundDataNewIfNull(mallInfo.getActivityId());
        userActivityFundData.addBuyMallKeyIds(mallKeyId);

        player.notifyListener(eGamePlayerEventType.CommonActivityFundProgress.getValue());

        syncUserData();
    }

    public void addProgress(int activityId, int goodsId, long addValue) {
        ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (openActivityInfo == null) {
            log.error("基金加进度，找不到活动{}", activityId);
            return;
        }
        UserActivityFundData userActivityFundData = getUserActivityFundDataNewIfNull(activityId);
        synchronized (this) {
            userActivityFundData.addProgress(addValue);
        }
        player.notifyListener(eGamePlayerEventType.CommonActivityFundProgress.getValue());
    }

    public long getProgress(int activityId) {
        UserActivityFundData userActivityFundData = userActivityFundDataMap.get(activityId);
        if (userActivityFundData == null) {
            return  -1;
        }
        return userActivityFundData.getProgress();
    }

    public void updateProgress(int activityId, long newValue) {
        ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (openActivityInfo == null) {
            log.error("基金加进度，找不到活动{}", activityId);
            return;
        }
        UserActivityFundData userActivityFundData = getUserActivityFundDataNewIfNull(activityId);
        userActivityFundData.setProgress(newValue);

        player.notifyListener(eGamePlayerEventType.CommonActivityFundProgress.getValue());
    }
}
